package com.infore.airsupervise.ui.widget;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.infore.airsupervise.R;
import com.infore.airsupervise.bean.SearchContainE;

import java.util.ArrayList;
import java.util.List;

/**
 * @features:
 * @author: WuTao
 * @date: 2017/4/12 10:15
 */
public class SearchContainView extends LinearLayout implements View.OnClickListener , TextWatcher{
    private RecyclerView swipeTarget;
    private EditText et_search;
    private TextView tv_cancal;
    private ImageView iv_clear;

    private List<SearchContainE> data= new ArrayList<>();//原数据集合
    private List<SearchContainE> cacheData = new ArrayList<>();//匹配搜索内容的集合数据

    private BaseQuickAdapter<SearchContainE,BaseViewHolder> adapter; //适配器

    private SearchContainItemListener listener;//接口对象
    private SearchContainViewListener vListener;
    /**
     * 处理控件监听的相关接口
     */
    public  interface  SearchContainItemListener{
        //RecyclerView item 点击事件
        void onItemClickListener(SearchContainE item);

    }

    public interface SearchContainViewListener{
        //其他空间的点击事件
        void onClickListener(View v);
    }

    private Context context;
    public SearchContainView(Context context , List<SearchContainE> list) {
        super(context);
        this.context = context;
        if(null != list){
            data.addAll(list);
            cacheData.addAll(list);
        }
        initView();
    }

    public SearchContainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    private void initView(){
        LayoutInflater.from(context).inflate(R.layout.fragment_search_contains,this);
        et_search = (EditText) findViewById(R.id.et_search);
        tv_cancal = (TextView) findViewById(R.id.tv_cancel);
        iv_clear = (ImageView) findViewById(R.id.iv_searchClean);
        swipeTarget = (RecyclerView) findViewById(R.id.swipe_target);
        swipeTarget.setLayoutManager( new LinearLayoutManager(context));
        swipeTarget.addItemDecoration(new ItemDecoration1px(context,ItemDecoration1px.VERTICAL_LIST));
        tv_cancal.setOnClickListener(this);
        iv_clear.setOnClickListener(this);
        et_search.addTextChangedListener(this);
        swipeTarget.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if(listener!=null){
                    listener.onItemClickListener(cacheData.get(position));
                }
            }
        });
        //RecyclerView 适配器 设置
        adapter = new BaseQuickAdapter<SearchContainE, BaseViewHolder>(R.layout.item_bottomdialog_search,cacheData) {
            @Override
            protected void convert(BaseViewHolder helper, SearchContainE item) {
                helper.setText(R.id.tv_search_title,item.getTitle());
            }
        };
        swipeTarget.setAdapter(adapter);
    }

    public void setItemData(List<SearchContainE> data){
        if(data!=null){
            this.data.clear();
            this.data.addAll(data);
            cacheData.addAll(data);
        }
    }
    public void updata(){
        adapter.notifyDataSetChanged();
    }

    /**
     * 设置事件监听
     * @param listener
     */
    public void setSearchContainListener(SearchContainItemListener listener){
        this.listener = listener;
    }

    public void setSearchContainViewListener(SearchContainViewListener listener){
        this.vListener = listener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_cancel :
                if(vListener!=null){
                    vListener.onClickListener(view);
                }
                break;
            case R.id.iv_searchClean :
                et_search.getText().clear();
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if(data!=null){
            String key = editable.toString();
            if(!TextUtils.isEmpty(key)){
                findData(key);
            }else{
                cacheData.clear();
                cacheData.addAll(data);
                adapter.notifyDataSetChanged();
            }
        }
    }

    private void findData(String key){
        cacheData.clear();
        for(SearchContainE e  : data){
               if(e.getTitle().toLowerCase().contains(key.toLowerCase())){
                    cacheData.add(e);
               }
        }
        adapter.notifyDataSetChanged();
    }


}
